package com.sevenGroup.pawnshop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sevenGroup.pawnshop.controller.form.AccountAndUserForm;
import com.sevenGroup.pawnshop.dao.RolesDao;
import com.sevenGroup.pawnshop.dao.StoresDao;
import com.sevenGroup.pawnshop.dao.UsersDao;
import com.sevenGroup.pawnshop.model.Account;
import com.sevenGroup.pawnshop.dao.AccountDao;
import com.sevenGroup.pawnshop.model.Roles;
import com.sevenGroup.pawnshop.model.Stores;
import com.sevenGroup.pawnshop.service.AccountService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author sevenGroup
 * @since 2022-02-16
 */
@Service("accountServiceImpl")
public class AccountServiceImpl extends ServiceImpl<AccountDao, Account> implements AccountService {
    @Resource
    private AccountDao accountDao;
    @Resource
    private UsersDao usersDao;
    @Resource
    private StoresDao storesDao;
    @Resource
    private RolesDao rolesDao;

    //  新增账号,需要做事务控制
    @Override
    @Transactional
    public Integer addAccount(AccountAndUserForm accountForm) {
//        密码需要设置编码
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String encode = bCryptPasswordEncoder.encode(accountForm.getAccountPassword());
        accountForm.setAccountPassword(encode);
//        新增账号，并且需要得到新增账号的id
        Integer num1 = accountDao.addAccount(accountForm);
//        通过账号名查询账号ID,并设置进去
        Integer accountId = accountDao.getAccountIdByAccountUsername(accountForm.getAccountUsername());
        accountForm.setAccountId(accountId);
//        如果是总经理，门店ID设置为0，门店名设置为空
        if (accountForm.getRoleId() == 2) {
            accountForm.setStoreId(0);
            accountForm.setStoreName("");
        } else {
            QueryWrapper<Stores> storesQueryWrapper = new QueryWrapper<>();
            Stores stores = storesDao.selectOne(storesQueryWrapper.eq("store_id",accountForm.getStoreId()).select("store_name"));
            accountForm.setStoreName(stores.getStoreName());
        }
//        通过角色查角色名
        Roles roles = rolesDao.selectById(accountForm.getRoleId());
        accountForm.setRoleName(roles.getRoleName());
        accountForm.setRoleManage(roles.getRoleManage());

//        新增用户
        Integer num2 = usersDao.addUser(accountForm);
        return num2;
    }

    //  查询是否有某个账号(通过账号)
    @Override
    public Boolean getAccountByAccountUsername(String accountUsernam) {
        Account account = accountDao.selectOne(new QueryWrapper<Account>().eq("account_username", accountUsernam).select("account_username"));
        if (null == account) return false;
        return true;
    }

    //  查询是否有某个手机号(手机号)
    @Override
    public Boolean getAccountByAccountTel(String accountTel) {
        Account account = accountDao.selectOne(new QueryWrapper<Account>().eq("account_tel", accountTel).select("account_tel"));
        if (null == account) return false;
        return true;
    }

    //  根据账号ID修改账号
    @Override
    public String updateAccount(Account account) {
//        先查询数据库有没有存在相同的账号或者手机号
        Account accountByUsername = accountDao.selectOne(new QueryWrapper<Account>().eq("account_username", account.getAccountUsername()));
        if (null != accountByUsername && accountByUsername.getAccountId() != account.getAccountId()) return "已存在相同用户名";
        Account accountByTel = accountDao.selectOne(new QueryWrapper<Account>().eq("account_tel", account.getAccountTel()));
        if (null != accountByTel && accountByTel.getAccountId() != account.getAccountId()) return "该手机号已被使用";
//        先加密
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        account.setAccountPassword(encoder.encode(account.getPassword()));
        int i = accountDao.updateById(account);
        if (i == 1) return "修改成功";
        else return "服务器繁忙！";
    }
}
